import {Component} from '@angular/core';
import {Events, IonicPage, NavController, NavParams, ToastController} from 'ionic-angular';
import {ActivityFilterModel} from "../../../model/pages/activity/activity-filter/activity-filter.model";
import {DateService} from "../../../services/common/date.service";

@IonicPage()
@Component({
    selector: 'page-team-market-activity-filter',
    templateUrl: 'team-market-activity-filter.html',
    providers: [
        DateService
    ]
})

export class TeamMarketActivityFilterPage {

    pickerOption = {
        cssClass: 'activity-filter_picker'
    };

    private filter ;
    private allTypes;
    private classify;

    private canSubmit: boolean = true;
    // 年度列表
    private yearList: any[] = [];

    constructor(private events: Events,
                private navCtrl: NavController,
                private navParams: NavParams,
                private toastCtrl: ToastController,
                public dateService: DateService) {
        this.classify = this.navParams.get('classify');
        this.allTypes = this.navParams.get('allTypes');
        this.filter = this.navParams.get('filter');
        this.yearList = this.dateService.getYearList(2010);
    }

    searchActivity() {
        this.events.publish('marketActivity:search', this.filter);
        this.navCtrl.pop();
    }

    setFilter(key, val) {
        let index = this.filter[key].indexOf(val);
        if (index === -1) {
            this.filter[key].push(val);
        } else {
            this.filter[key].splice(index, 1);
        }
    }

    resetFilter() {
        this.filter.type = [];
        this.filter.status = [1];
        this.filter.startTime = this.dateService.getCurrentMonthFirstDay();
        this.filter.endTime = this.dateService.getCurrentMonthLastDay();
        this.filter.groupId='';
    }

    clearShow() {
        return this.filter.endTime || this.filter.startTime;
    }

    clearDate() {
        this.filter.endTime = this.filter.startTime = null;
    }

    validateForm() {
        if (this.filter.startTime && this.filter.endTime
            && new Date(this.filter.startTime) > new Date(this.filter.endTime)) {
            this.toastCtrl.create({
                duration: 1500,
                position: 'middle',
                message: '查询开始时间输入有误'
            }).present();
            this.canSubmit = false;
        } else {
            this.canSubmit = true;
        }
    }
}
